package com.surelution.projects.crm

class PromotingController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [promotingInstanceList: Promoting.list(params), promotingInstanceTotal: Promoting.count()]
    }

	def search = {
        def gasStation = GasStation.get(params.id)
		def domain
		if (OwnStation.get(gasStation.id)) domain = "ownStation"
		else if (CompetitorStation.get(gasStation.id)){
			domain = "competitorStation"
		}
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		def promoting = Promoting.createCriteria().list([max:params.max, offset:params.offset]) {
			createAlias("stationGas", "s")
			eq('s.station', gasStation)
			order("endDate", "desc")
		}
		[promotingInstanceList: promoting,domain:domain,stationId:gasStation.id]
    }
	
    def create = {
        def promotingInstance = new Promoting()
        promotingInstance.properties = params
        return [promotingInstance: promotingInstance]
    }

    def save = {
        def promotingInstance = new Promoting(params)
        if (promotingInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'promoting.label', default: 'Promoting'), promotingInstance.id])}"
			if(promotingInstance.stationGas.station instanceof OwnStation) {
            	flash.message = "${message(code: 'default.created.message', args: [message(code: 'promoting.label', default: 'Promoting'), promotingInstance.id])}"
				redirect(controller: "ownStation",action: "show",id: promotingInstance.stationGas.station.id)
			} else if(promotingInstance.stationGas.station instanceof CompetitorStation) {
            	flash.message = "${message(code: 'default.created.message', args: [message(code: 'promoting.label', default: 'Promoting'), promotingInstance.id])}"
				redirect(controller: "competitorStation",action: "show",id: promotingInstance.stationGas.station.id)
			}
        }
        else {
            render(view: "create", model: [promotingInstance: promotingInstance])
        }
    }

    def show = {
        def promotingInstance = Promoting.get(params.id)
        if (!promotingInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'promoting.label', default: 'Promoting'), params.id])}"
            redirect(action: "list")
        }
        else {
            [promotingInstance: promotingInstance]
        }
    }

    def edit = {
        def promotingInstance = Promoting.get(params.id)
        if (!promotingInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'promoting.label', default: 'Promoting'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [promotingInstance: promotingInstance]
        }
    }

    def update = {
        def promotingInstance = Promoting.get(params.id)
        if (promotingInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (promotingInstance.version > version) {
                    
                    promotingInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'promoting.label', default: 'Promoting')] as Object[], "Another user has updated this Promoting while you were editing")
                    render(view: "edit", model: [promotingInstance: promotingInstance])
                    return
                }
            }
            promotingInstance.properties = params
            if (!promotingInstance.hasErrors() && promotingInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'promoting.label', default: 'Promoting'), promotingInstance.id])}"
                redirect(action: "show", id: promotingInstance.id)
            }
            else {
                render(view: "edit", model: [promotingInstance: promotingInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'promoting.label', default: 'Promoting'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def promotingInstance = Promoting.get(params.id)
        if (promotingInstance) {
            try {
                promotingInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'promoting.label', default: 'Promoting'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'promoting.label', default: 'Promoting'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'promoting.label', default: 'Promoting'), params.id])}"
            redirect(action: "list")
        }
    }
}
